package com.angel.kuaidi.activity;

import com.angel.kuaidi.R;
import com.angel.kuaidi.KuaidiApp;
import com.angel.kuaidi.KuaidiConstans;
import com.angel.kuaidi.analytics.Analytics;
import com.angel.kuaidi.service.UpdateCheckService;
import com.angel.kuaidi.utils.UtilsLog;
import com.angel.kuaidi.view.BaseLayout;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;

public abstract class BaseActivity extends Activity implements OnClickListener, KuaidiConstans {

	public BaseActivity() {
		mContext = this;
		mApp = KuaidiApp.getSelf();

	}

	protected void checkUpdate() {

		new UpdateCheckService(this).start();
	}

	protected Context mContext;
	protected KuaidiApp mApp;

	protected BaseLayout baseLayout;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (getParent() != null) {
				getParent().onKeyDown(keyCode, event);
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.ll_header_left:
			onLeftBtnClick();
			//

			break;
		case R.id.ll_header_right:
			onRightBtnClick();
			break;
		default:
			break;
		}
		UtilsLog.i("msg", "fffff");
	}

	/**
	 * 设置布局
	 * 
	 * @param layoutResId
	 *            布局id
	 */
	protected void setView(int layoutResId) {
		baseLayout = new BaseLayout(mContext, layoutResId);
		setContentView(baseLayout);

	}

	/**
	 * 自定义标题
	 * 
	 * @param left
	 *            左边
	 * @param title
	 *            中间标题
	 */
	protected void setTitle(String left, String title) {
		setTitle(title);
		setLeftBtnText(left);
		setRightBtnText("");
	}

	/**
	 * 自定义标题
	 * 
	 * @param left
	 *            左边按钮
	 * @param title
	 *            标题
	 * @param right
	 *            右边按钮
	 */
	protected void setTitle(String left, String title, String right) {

		setTitle(title);
		setLeftBtnText(left);
		setRightBtnText(right);
	}

	/**
	 * 设置标题
	 * 
	 * @param title
	 */
	private void setTitle(String title) {
		baseLayout.setTitle(title);
		setLeftBtnText("");
		setRightBtnText("");
	}

	/**
	 * 设置左边按钮
	 */
	private void setLeftBtnText(String text) {

		if (text != null && !"".equals(text) && baseLayout != null) {
			baseLayout.setLeftBtnText(text);
		}
	}

	/**
	 * 设置右边按钮
	 */
	private void setRightBtnText(String text) {

		if (text != null && !"".equals(text) && baseLayout != null) {
			baseLayout.setRightBtnText(text);
		}
	}

	/**
	 * 处理标题左边按钮
	 */
	protected void onLeftBtnClick() {
		finish();
	}

	/**
	 * 处理标题右边按钮
	 */
	protected void onRightBtnClick() {

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		if (mContext == this) {
			super.onCreate(savedInstanceState);
		}
	}

	/**
	 * 检查软件版本更新信息
	 * 
	 * @return
	 */
	protected void checkForUpDate() {

		// mApp.getUpdateManager().checkForUpDate();
	}

	@Override
	protected void onResume() {
		super.onResume();
		Analytics.onResume(mContext);
	}

	@Override
	protected void onPause() {
		super.onPause();
		Analytics.onPause(mContext);
	}
}
